package edu.columbia.cloudbox.ui;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import edu.columbia.cloudbox.R;
import edu.columbia.cloudbox.UserStatusService;
import edu.columbia.cloudbox.io.FileType;
import edu.columbia.cloudbox.io.LocalIO;
import edu.columbia.cloudbox.policy.CBDBHelper;

public class SavedFilesAdapter extends ArrayAdapter<FileType> {

	private final static String LOG_TAG = "SavedFilesAdapter";

	private final Context context;
	private final List<FileType> files;

	public SavedFilesAdapter(Context context, List<FileType> files) {
		super(context, R.layout.saved_file_row, files);
		this.context = context;
		this.files = files;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater inflator = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		// Get the row of list item
		View rowView = inflator.inflate(R.layout.saved_file_row, parent, false);
		
		// Current item(file) in the list
		final FileType file = files.get(position);

		// Set name of file
		TextView textView = (TextView) rowView.findViewById(R.id.savedfilename);
		final String fileName = file.getName();
		textView.setText(fileName);

		// Set image
		ImageView imageView = (ImageView) rowView
				.findViewById(R.id.savedfileicon);

		final boolean isFolder = files.get(position).isFolder();
		// String type = files.get(position).getType();

		if (isFolder) {
			imageView.setImageResource(R.drawable.icon_folder);
		} else {
			imageView.setImageResource(R.drawable.icon_doc);
		}
		//
		// Set tag<isFolder> in the row
		rowView.setTag(isFolder);
		
		/** Set onClick Listener -- Delete file/dir from local **/
		Button deleteBtn = (Button) rowView.findViewById(R.id.btn_delete);
		deleteBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i(LOG_TAG, "Button Clicked");
				// Make path for downloading file
				String path = FilePathOnTab.makePath(FilePathOnTab.currentPath_savedFiles);
				boolean result = false;
				if (!isFolder) {
					result = delete(UserStatusService.USER_NAME, path, fileName);
					CBDBHelper dbHelper = new CBDBHelper(context);
					String fileKey = "";
					if ("".equals(path))
						fileKey = UserStatusService.USER_NAME + "/" + fileName;
					else
						fileKey = UserStatusService.USER_NAME + "/" + path + "/" + fileName;
					dbHelper.deleteFile(fileKey);
					dbHelper.close();
				}
				else {
					CBDBHelper dbHelper = new CBDBHelper(context);
					List<String> files = getAllFilesByFolder(UserStatusService.USER_NAME, path, fileName);
					dbHelper.deleteFolder(files);
					dbHelper.close();
					result = deleteFolder(UserStatusService.USER_NAME, path, fileName);
				}
				remove(file);
				Log.i(LOG_TAG, "download result: " + result);
			}
		});
		return rowView;
	}
	
	/**
	 * Delete file from local storage
	 * 
	 * @param userName
	 * @param path
	 * @param fileName
	 * @return
	 */
	private boolean delete(String userName, String path, String fileName) {
		boolean result = false;
		LocalIO localIO = UserStatusService.LocalIO_INSTANCE;
		if ("".equals(path))
			result = localIO.delete(userName + "/" + fileName);
		else
			result = localIO.delete(userName + "/" + path + "/" + fileName);
		return result;
	}
	
	/**
	 * Delete folder from local storage
	 * 
	 * @param userName
	 * @param path
	 * @param folderName
	 * @return
	 */
	private boolean deleteFolder(String userName, String path, String folderName) {
		boolean result = false;
		LocalIO localIO = UserStatusService.LocalIO_INSTANCE;
		if ("".equals(path))
			result = localIO.deleteFolder(userName + "/" + folderName);
		else
			result = localIO.deleteFolder(userName + "/" + path + "/" + folderName);
		return result;
	}
	
	/**
	 * Browse a folder from local storage
	 * 
	 * @param userName
	 * @param path
	 * @param folderName
	 * @return
	 */
	private List<String> getAllFilesByFolder(String userName, String path, String folderName) {
		List<String> files = null;
		LocalIO localIO = UserStatusService.LocalIO_INSTANCE;
		if ("".equals(path))
			files = localIO.getPathsByFolder(userName + "/" + folderName);
		else
			files = localIO.getPathsByFolder(userName + "/" + path + "/" + folderName);
		return files;
	}
}
